package com.tiancheng.onecode.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tiancheng.onecode.trade.configuration.TradeApplicationProperties;
import com.tiancheng.onecode.model.request.ApplicationAccessTokenRequestVO;
import com.tiancheng.onecode.trade.model.request.order.OpenCancelOrderVO;
import com.tiancheng.onecode.trade.model.request.order.OpenCreateOrderBaseVO;
import com.tiancheng.onecode.trade.model.request.order.OpenOrderPayVO;
import com.tiancheng.onecode.trade.model.request.order.OpenOrderPrePayResVO;
import com.tiancheng.onecode.model.response.ApplicationAccessTokenResponseVO;
import com.tiancheng.onecode.model.response.Result;
import com.tiancheng.onecode.service.ITradeService;
import com.tiancheng.onecode.trade.utils.ForestHttpUtil;
import com.tiancheng.onecode.utils.JsonUtil;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.message.BasicHeader;
import org.springframework.stereotype.Service;

/**
 * @Author: kellen
 * @Description: 交易接口实现
 * @Date: create in 2024/12/7 15:08
 */
@Slf4j
@Service
public class TradeServiceImpl implements ITradeService {

    @Resource
    private TradeApplicationProperties tradeApplicationProperties;


    @Override
    public ApplicationAccessTokenResponseVO getAccessToken(ApplicationAccessTokenRequestVO requestVO) {

        final String s = ForestHttpUtil.getInstance().doPostJson(tradeApplicationProperties.getHosts() + "/trade-auth/auth/loginIn/appAccessToken", requestVO, null);
        log.info("登录认证结果：{}",s);
        if (StringUtils.isNotEmpty(s)) {
            final Result result = JsonUtil.fromJson(s, Result.class);
            if (result.getRet() == 0 && null != result.getData()) {
                return JsonUtil.fromJson(result.getData(), new TypeReference<ApplicationAccessTokenResponseVO>() {
                });
            }
            throw new RuntimeException("登录失败");
        }
        return null;
    }

    @Override
    public String createOrder(OpenCreateOrderBaseVO createOrderBaseVO, String authToken) {
        final String s = ForestHttpUtil.getInstance().doPostJson(tradeApplicationProperties.getHosts() + "/trade-order/open/create", createOrderBaseVO, new BasicHeader("token", authToken));
        log.info("下单返回：{}",s);
        if (StringUtils.isNotEmpty(s)) {
            final Result result = JsonUtil.fromJson(s, Result.class);
            if (result.getRet() == 0 && null != result.getData()) {
                return result.getData().toString();
            }
            throw new RuntimeException("下单失败，error:"+result.getMsg());
        }
        return null;
    }

    @Override
    public Boolean close(OpenCancelOrderVO openCancelOrderVO, String authToken) {
        final String s = ForestHttpUtil.getInstance().doPostJson(tradeApplicationProperties.getHosts() + "/trade-order/open/close", openCancelOrderVO, new BasicHeader("token", authToken));
        log.info("关闭订单返回：{}",s);
        if (StringUtils.isNotEmpty(s)) {
            final Result result = JsonUtil.fromJson(s, Result.class);
            return result.getRet() == 0;
        }
        return false;
    }

    @Override
    public OpenOrderPrePayResVO pay(OpenOrderPayVO openOrderPayVO, String authToken) {
        final String s = ForestHttpUtil.getInstance().doPostJson(tradeApplicationProperties.getHosts() + "/trade-order/open/pay", openOrderPayVO, new BasicHeader("token", authToken));
        log.info("支付订单返回：{}",s);
        if (StringUtils.isNotEmpty(s)) {
            final Result result = JsonUtil.fromJson(s, Result.class);
            if (result.getRet() == 0 && null != result.getData()) {
                return JsonUtil.fromJson(result.getData(), new TypeReference<OpenOrderPrePayResVO>() {
                });
            }
            throw new RuntimeException("拉起支付失败，error:"+result.getMsg());
        }
        return null;
    }
}
